package com.itheima.edu.info.subwaymanager.dao;

import com.itheima.edu.info.subwaymanager.domain.Subway;
import com.itheima.edu.info.subwaymanager.ioutil.SubwayIOUtil;

import java.util.ArrayList;

/**
 * @Author 王加鑫
 * @Date 2021/5/23 19:55
 * @Version 1.0
 */
public class SubwayDaoImpl implements SubwayDao {

    /**
     * 地铁信息集合容器
     */
    private static ArrayList<Subway> subways = new ArrayList<>();

    @Override
    public ArrayList<Subway> selectSubway() {
        subways = SubwayIOUtil.readMessage();
        return subways;

    }

    @Override
    public void addSubway(Subway subway) {
        SubwayIOUtil.writeMessage(subway);
    }

    @Override
    public void updateSubway() {

    }

    @Override
    public void deleteSubway() {

    }

    /*
    根据地铁编号查找容器中是否存在
     */
    @Override
    public int getIndex(String id) {
        for (int i = 0; i < subways.size(); i++) {
            if(id.equals(subways.get(i))){
                return i;
            }
        }
        return -1;
    }
}
